<template>
  <div>
    <h1 style="color: orange">稿件管理页面</h1>
    <el-radio-group v-model="type" @input="handleInput()">
      <el-radio-button label="1">烘焙食谱</el-radio-button>
      <el-radio-button label="5">烘焙视频</el-radio-button>
      <el-radio-button label="8">行业资讯</el-radio-button>
    </el-radio-group>
    <el-button type="success" @click="post()">发布内容</el-button>
    <el-table :data="contentArr">
      <el-table-column type="index" label="编号"></el-table-column>
      <el-table-column prop="title" label="商品标题" width="100px"></el-table-column>
      <el-table-column prop="brief" label="商品摘要" width="300px"></el-table-column>
      <el-table-column prop="categoryName" label="商品类型"></el-table-column>
      <el-table-column label="商品封面" width="100px">
        <template slot-scope="scope">
          <!--scope.row得到当前行对应的对象  /2023/09/14/xxxx.jpg-->
          <img :src="BASE_URL+scope.row.imgUrl" width="80px">
        </template>
      </el-table-column>
      <el-table-column prop="viewCount" label="浏览量"></el-table-column>
      <el-table-column prop="commentCount" label="评论量"></el-table-column>
      <el-table-column prop="createTime" label="发布时间"></el-table-column>
      <!-- fixed="right" 固定到右侧-->
      <el-table-column label="操作" width="200px" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="success"
                     @click="handleEdit(scope.$index, scope.row)">修改
          </el-button>
          <el-button size="mini" type="danger"
                     @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: "PersonalManagementView",
  data() {
    return {
      type: 1,
      contentArr: []
    }
  },
  methods: {
    handleEdit(i,c){
      //跳转到发布页面,把内容id传递过去
      this.$router.push('/personal/post?id='+c.id);
    },
    handleDelete(i,c){
      if (confirm("您确认删除此内容吗?")){
        this.axios.post(this.BASE_URL+'/v1/contents/'+c.id+"/delete")
        .then((response)=>{
          if (response.data.code==1){
            //删除数组中的数据让页面发生改变
            this.contentArr.splice(i,1);
          }
        })
      }
    },
    handleInput() {
      this.loadContents();
    },
    post() {
      //显示发布页面
      this.$router.push('/personal/post');
    },
    loadContents() {
      //请求当前分类下并且是当前登录用户发布的内容
      let user = localStorage.user ? JSON.parse(localStorage.user) : null;
      this.axios.get(this.BASE_URL + '/v1/contents/'
          + this.type + '/' + user.id + '/management')
          .then((response) => {
            if (response.data.code == 1) {
              this.contentArr = response.data.data;
            }
          })
    }
  },
  mounted() {
    this.loadContents();
  }
}
</script>

<style scoped>

</style>